package com.sinohantech.schedule.job.admin.controller;

import com.sinohantech.schedule.job.admin.dao.ScheduleJobGroupDao;
import com.sinohantech.schedule.job.admin.core.model.ScheduleJobGroup;
import com.sinohantech.schedule.job.admin.core.util.I18nUtil;
import com.sinohantech.schedule.job.admin.dao.ScheduleJobInfoDao;
import com.sinohantech.schedule.job.core.biz.model.ReturnT;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * job group controller
 * @author opensource 2016-10-02 20:52:56
 */
@Controller
@RequestMapping("/jobgroup")
public class JobGroupController {

	@Resource
	public ScheduleJobInfoDao scheduleJobInfoDao;
	@Resource
	public ScheduleJobGroupDao scheduleJobGroupDao;

	@RequestMapping
	public String index(Model model) {

		// job group (executor)
		List<ScheduleJobGroup> list = scheduleJobGroupDao.findAll();

		model.addAttribute("list", list);
		return "jobgroup/jobgroup.index";
	}

	@RequestMapping("/save")
	@ResponseBody
	public ReturnT<String> save(ScheduleJobGroup scheduleJobGroup){

		// valid
		if (scheduleJobGroup.getAppName()==null || StringUtils.isBlank(scheduleJobGroup.getAppName())) {
			return new ReturnT<String>(500, (I18nUtil.getString("system_please_input")+"AppName") );
		}
		if (scheduleJobGroup.getAppName().length()<4 || scheduleJobGroup.getAppName().length()>64) {
			return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_appName_length") );
		}
		if (scheduleJobGroup.getTitle()==null || StringUtils.isBlank(scheduleJobGroup.getTitle())) {
			return new ReturnT<String>(500, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title")) );
		}
		if (scheduleJobGroup.getAddressType()!=0) {
			if (StringUtils.isBlank(scheduleJobGroup.getAddressList())) {
				return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_addressType_limit") );
			}
			String[] addresss = scheduleJobGroup.getAddressList().split(",");
			for (String item: addresss) {
				if (StringUtils.isBlank(item)) {
					return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_registryList_unvalid") );
				}
			}
		}

		int ret = scheduleJobGroupDao.save(scheduleJobGroup);
		return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
	}

	@RequestMapping("/update")
	@ResponseBody
	public ReturnT<String> update(ScheduleJobGroup scheduleJobGroup){
		// valid
		if (scheduleJobGroup.getAppName()==null || StringUtils.isBlank(scheduleJobGroup.getAppName())) {
			return new ReturnT<String>(500, (I18nUtil.getString("system_please_input")+"AppName") );
		}
		if (scheduleJobGroup.getAppName().length()<4 || scheduleJobGroup.getAppName().length()>64) {
			return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_appName_length") );
		}
		if (scheduleJobGroup.getTitle()==null || StringUtils.isBlank(scheduleJobGroup.getTitle())) {
			return new ReturnT<String>(500, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title")) );
		}
		if (scheduleJobGroup.getAddressType()!=0) {
			if (StringUtils.isBlank(scheduleJobGroup.getAddressList())) {
				return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_addressType_limit") );
			}
			String[] addresss = scheduleJobGroup.getAddressList().split(",");
			for (String item: addresss) {
				if (StringUtils.isBlank(item)) {
					return new ReturnT<String>(500, I18nUtil.getString("jobgroup_field_registryList_unvalid") );
				}
			}
		}

		int ret = scheduleJobGroupDao.update(scheduleJobGroup);
		return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
	}

	@RequestMapping("/remove")
	@ResponseBody
	public ReturnT<String> remove(int id){

		// valid
		int count = scheduleJobInfoDao.pageListCount(0, 10, id, null, null);
		if (count > 0) {
			return new ReturnT<String>(500, I18nUtil.getString("jobgroup_del_limit_0") );
		}

		List<ScheduleJobGroup> allList = scheduleJobGroupDao.findAll();
		if (allList.size() == 1) {
			return new ReturnT<String>(500, I18nUtil.getString("jobgroup_del_limit_1") );
		}

		int ret = scheduleJobGroupDao.remove(id);
		return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
	}

}
